// pages/calendar/calendar.js
const app=getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        year:0,
        month:0,
        date:['日','一','二','三','四','五','六'],
        dateArr:[],
        isToday:0,
        isTodayWeek:false,
        todayIndex:0,
        inputVal:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        this.setData({
          year: year,
          month: month,
          isToday: '' + year + month + now.getDate()
          
      })
        this.dateInit();
        
        
        
    },
    dateInit: function (setYear, setMonth) {
        let dateArr1 = [];                       
        let arrLen = 0;                        
        let now = setYear ? new Date(setYear, setMonth) : new Date();
        let year = setYear || now.getFullYear();
        let nextYear = 0;
        let month = setMonth || now.getMonth();                 
        let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
        let startWeek = new Date(year + ',' + (month + 1) + ',' + 1).getDay();  
        let dayNums = new Date(year, nextMonth, 0).getDate();              
        let obj = {};
        let num = 0;
        
        if (month + 1 > 11) {
          nextYear = year + 1;
          dayNums = new Date(nextYear, nextMonth, 0).getDate();
        }
        arrLen = startWeek + dayNums;
        for (let i = 0; i < arrLen; i++) {
          if (i >= startWeek) {
            num = i - startWeek + 1;
            obj = {
              isToday: '' + year + (month + 1) + num,
              istime:year +'年'+ (month+1) +"月"+ num+"日",
              dateNum: num,
              weight: 5,
              color:'white'
            }
       
          } else {
            obj = {};
          }
          dateArr1[i] = obj;
          
          if(dateArr1[i].isToday==this.data.isToday){
            dateArr1[i].color='#ff9933'
            
          }
         
        }
        this.setData({
          dateArr: dateArr1
        })
        
       
      },
     
      changeMonth(){
        let year = this.data.month - 1 == 0 ? this.data.year - 1 : this.data.year;
        let month = this.data.month - 1 == 0 ? 11 : this.data.month - 2;
        this.setData({
          year: year,
          month: (month + 1)
        })
        this.dateInit(year, month);
      },
      changeMonth1(){
        let year = this.data.month ==12 ? this.data.year + 1 : this.data.year;
        let month = this.data.month ==12 ? 0 : this.data.month;
        this.setData({
          year: year,
          month: (month + 1)
        })
        this.dateInit(year, month);
      },
      changetap(e){
        let data=e.currentTarget.dataset.year.toString()+e.currentTarget.dataset.month.toString()+e.currentTarget.dataset.datenum.toString()
        let arr=this.data.dateArr
        for(let i=0;i<this.data.dateArr.length;i++){
          if(this.data.dateArr[i].isToday==data ){
            arr[i].color= '#ff9933'
            this.setData({
              dateArr:arr
            })
          }else{
            arr[i].color='white'
            this.setData({
              dateArr:arr
            })
          }
        }
      },
      delete(){
        this.setData({
          inputVal:""
      })
      },
      saveVal(e){
        this.setData({
          inputVal:e.detail.value
      })
      },
      search(){
        let now = new Date();
        let time=this.data.inputVal.split('月')
        console.log(time)
        if(time.length>1){
          let year=this.data.inputVal.slice(0,4)
          let month = time[0].substring(5,time[0].length)
          let num = time[1].substring(0,time[1].length-1)
          this.setData({
            year:Number(year),
            month:Number(month)
          })
          this.dateInit(Number(year),Number(month)-1);
          this.datalist(year,month,num)
        }else{
          wx.showModal({
            title:'输入日期格式应为 *年*月*日或者*年*月',
            icon:'none'
          })
        }
        
      },
      datalist(year,month,num){
        let arr=this.data.dateArr
        for(let i=0;i<this.data.dateArr.length;i++){
           if( this.data.dateArr[i].isToday==year+month+num   ){
             arr[i].color='#ff9933'
             this.setData({dateArr:arr})
            }else{
              arr[i].color='white'
              this.setData({dateArr:arr})
            }
         }
      },
      showContent(){
        for(let i =0;i<this.data.dateArr.length;i++){
          if(this.data.dateArr[i].color=='#ff9933'){
            
            wx.navigateTo({
              url: '/pages/detail/detail?date='+this.data.dateArr[i].istime,
            })
            
          }
        }

        
        
       
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad962ab3f51d91e21dd0.jpg'
            })
        }
        let children=wx.getStorageSync('children')
        if(children!==''){
            this.setData({
                children:wx.getStorageSync('children')
            })
        }else{
            this.setData({
                children:'https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171d6.png'
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})